package com.pai4j.sso.controller;


import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.domain.vo.request.request.UserLoginInfoRequestVO;
import com.pai4j.domain.vo.request.request.UserRegistryInfoRequestVO;
import com.pai4j.log.annotation.OperationLog;
import com.pai4j.remote.sso.OauthServiceClient;
import com.pai4j.sso.service.SSOCoreService;
import com.pai4j.sso.service.ValidCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class SSOServiceController implements OauthServiceClient{

    @Autowired
    private SSOCoreService ssoCoreService;
    @Autowired
    private ValidCodeService validCodeService;


    @OperationLog(
        module = "SSO管理",
        operation = "SSO登录",
        description = "用户通过SSO进行登录",
        recordParams = true,
        ignoreParams = {"password"}
    )
    public PAIResponseBean<String> login(@RequestBody UserLoginInfoRequestVO loginInfoRequest) {

        return PAIResponseBeanUtil.success(ssoCoreService.login(loginInfoRequest));
    }

    /**
     * 用户注册
     * @param request
     * @return
     */

    @OperationLog(
        module = "SSO管理",
        operation = "SSO注册",
        description = "用户通过SSO进行注册",
        recordParams = true,
        ignoreParams = {"password"}
    )
    public PAIResponseBean register(@RequestBody UserRegistryInfoRequestVO request) {
        // 数字验证码核验
        validCodeService.validCode(request.getPicCheckCode());
        boolean result = ssoCoreService.registry(request);
        return PAIResponseBeanUtil.success(result ? "注册成功" : "测试失败");
    }


    @Override
    @OperationLog(
        module = "SSO管理",
        operation = "获取用户ID",
        description = "通过授权码获取用户ID",
        recordParams = true
    )
    public PAIResponseBean<String> getUidByOauthCode(@PathVariable String oauthCode) {
        String uid = ssoCoreService.getUidByOauthCode(oauthCode);
        return PAIResponseBeanUtil.success(uid);
    }



    @Override
    @OperationLog(
            module = "SSO管理",
            operation = "生成登录授权码",
            description = "通过uid生成登录授权码",
            recordParams = true
    )
    public PAIResponseBean<String> genLoginOauthCode(@PathVariable("uid") String uid){
        String oauthCode = ssoCoreService.genLoginOauthCode(uid);
        return PAIResponseBeanUtil.success(oauthCode);
    }
}
